var vo={};
$(function(){
	var openWindow=window.document;
	var searchHeight=$("#divform").height();
	$("#listLevelModelPrice").height($(openWindow).height()-$(openWindow).height()*0.05-searchHeight-10);
	//动态设置改变操作列宽度
	var fcol=opt.split('</a>');
	var contrlColumn=0;
	if(fcol.length>0){
		contrlColumn=(fcol.length-1)*window.fcolMinWidth;
	}
	
	$("#listLevelModelPrice").datagrid({
					url:'/levelmodelprice/getLevelModelPrice',
					title:'返款设置列表',
					method:'get',
					pagination:true,
					pageList: window.PageSize,
					loadMsg:'加载中......',
					nowrap:true,
					rowStyler:function(index,row){   
				        if (row.isflag){   
				            return 'background-color:'+window.flagColor;   
				        }
				    },
					columns:[[
					         {field:'ck',checkbox:true},
							 {field:'id' ,align:'center',title:'操作',width:contrlColumn,formatter: function(value, row, index){
								 var re=new RegExp("{paramlevel}","g");
								 var ref=opt.replace(re,row.levelid);
								 re=new RegExp("{parammodel}","g");
								 ref=ref.replace(re,row.modelid);
								 return ref;
							 }},
							 {field:'levelname',title:'城市等级名称', align:'left',width:120},
					         {field:'modelid',title:'型号编号', align:'left',width:100},
					         {field:'modelname',title:'型号名称', align:'left',width:100},
					         {field:'price',title:'返款价格', align:'left',width:100},
					         {field:'remark',title:'备注', align:'left',width:200}
					     ]]
				});
	$("#serbutton").click(function(){
		vo.levelname=$("#levelname").val();
		vo.modelid=$("#modelid").val();
		vo.modelname=$("#modelname").val();
		$("#listLevelModelPrice").datagrid('load',vo);
	});
	$("#listLevelModelPrice").datagrid({
		toolbar: '#tb'
	});
	//绑定回车事件
	$(document).keydown(function(event){ 
		if(event.keyCode==13){
			$("#serbutton").click(); 
		}
		if(event.keyCode==46){
			var rows=$("#listLevelModelPrice").datagrid('getSelections');
			if(rows.length==1){
				delLevelModelPrice(rows[0].levelmodelpriceid);
			}
			else{
				betchDel();
			}
		}
		if(event.keyCode==67){
			var rows=$("#listLevelModelPrice").datagrid('getSelections');
			if(rows.length<=0){
				$.messager.alert("失败","选中的记录为空：",'error');
			}
			copyLevelModelPrice(rows[0].levelmodelpriceid);
		}
	})
	//如果操作项为空隐藏操作列
	if(opt==''){
		$("#listLevelModelPrice").datagrid('hideColumn','id');
	}
});
/**
 * 批量删除
 * @param idcard
 */
function betchDel(){
	var rows=$("#listLevelModelPrice").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.confirm('删除','您确定要删除选中的返款设置吗？',function(data){
		if(data){
			var levels=[];
			for(var i=0;i<rows.length;i++){
				var level={};
				level.levelid=rows[i].levelid;
				level.modelid=rows[i].modelid;
				levels.push(level);
			}
			$.ajax({
				type:'post',
				url: '/levelmodelprice/betchDel',
				data:{levels:levels},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","删除成功",'info',function(){
							$("#listLevelModelPrice").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","删除失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","删除失败：",'error');
				}
			});
		}
	})
}
/**
 * 删除
 * @param idcard
 */
function delLevelModelPrice(levelid,modelid){
	$.messager.confirm('删除','您确定要删除选中的返款设置吗？',function(data){
		if(data){
			$.ajax({
				type:'get',
				url: '/levelmodelprice/delLevelModelPrice',
				data:{levelid:levelid,modelid:modelid},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","删除成功",'info',function(){
							$("#listLevelModelPrice").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","删除失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","删除失败：",'error');
				}
			});
		}
	})
}
/**
 * 详情
 */
function viewLevelModelPrice(levelid,modelid){
	window.parent.openDialog("返款设置详情",'/levelmodelprice/viewLevelModelPrice','',400,400,'levelid='+levelid+'&modelid='+modelid);
}
/**
 * 新增
 */
function addLevelModelPrice(){
	window.parent.openDialog("新增返款设置",'/levelmodelprice/addLevelModelPrice','',400,350,'');
}
/**
 * 复制
 */
function copyLevelModelPrice(levelid,modelid){
	window.parent.openDialog("新增返款设置",'/levelmodelprice/copyLevelModelPrice','',400,350,'levelid='+levelid+'&modelid='+modelid);
}
/**
 * 编辑
 * @param id
 */
function edit(levelid,modelid){
	window.parent.openDialog("修改返款设置",'/levelmodelprice/editLevelModelPrice','',400,350,'levelid='+levelid+'&modelid='+modelid);
}
/**
 * 导出为excel
 */
function exportXls(){
	vo.devicename=getVal(vo.devicename);
	vo.deviceid=getVal(vo.deviceid);
	var url='/levelmodelprice/exportXls?deviceid='+vo.deviceid+'&devicename'+vo.devicename;
	window.open(url,'','',false);
}
